<template>
  <div class="container">

    <div class="login-form">
        <el-form
      label-width="100px"
      ref="ruleFormRef"
      :rules="rules"
      :model="ruleForm"
      style="max-width: 460px"
      
    >
      <el-form-item>
        <h1 class="login-title">厚溥云课堂后台管理系统</h1>
      </el-form-item>
      <el-form-item prop="username">
        <el-input  v-model="ruleForm.username" placeholder="用户名"  :prefix-icon="User"/>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="ruleForm.password" placeholder="密码"
        :prefix-icon="Lock" show-password/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width: 100%" @click="submitForm(ruleFormRef)">登录</el-button>
      </el-form-item>
    </el-form>
    </div>
    
  </div>
</template>
  
  <script lang="ts" setup>
import { reactive, ref } from "vue";

import userApi from '../api/user'

import { useRouter } from "vue-router";

import type { FormInstance, FormRules } from 'element-plus'
import { User,Lock } from '@element-plus/icons-vue'
const router=useRouter()

const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  username: '',
  password: '',
  
})

const rules = reactive<FormRules>({
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  region: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'bluer',
    },
  ]
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      //调用api
      userApi.login( ruleForm.username,ruleForm.password).then(res=>{
        //登录成功

        //跳转至首页
        router.push('/')
      }).catch
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}


</script>

<style scoped>
.login-title {
  width: 100%;
  text-align: center;
  color: white;
}
.container{
   width: 100vw;
   height: 100vh;
   background-color: #2f3a4c;
}

.login-form{
    position: relative;
    top:200px;
    left: 450px;
    width: 740px;
}
</style>
  